﻿Namespace TaskList
    Public Class HomeController
        Inherits System.Web.Mvc.Controller

        '
        ' GET: /Home

        Private db As New TaskListDataContext()
        'Display a list of tasks
        Function Index() As ActionResult
            Dim tasks = From t In db.Tasks Order By t.EntryDate Descending
            Return View(tasks.ToList())
        End Function

        ' Display a form for creating a new task
        Function Create() As ActionResult
            Return View()
        End Function

        ' Add a new task to the database
        Function CreateNew(ByVal task As String) As ActionResult
            ' Add the new task to database
            Dim newTask As New Task()
            newTask.Task = task
            newTask.IsCompleted = False
            newTask.EntryDate = DateTime.Now
            db.Tasks.InsertOnSubmit(newTask)
            db.SubmitChanges()
            Return RedirectToAction("Index")
        End Function

        ' Mark a task as complete
        Function Complete(ByVal Id As Integer) As ActionResult
            Dim tasks = From t In db.Tasks Where t.ID = Id
            For Each Match In tasks
                Match.IsCompleted = True
            Next
            db.SubmitChanges()
            Return RedirectToAction("Index")
        End Function
        ' Mark a task as uncomplete
        Function UnComplete(ByVal Id As Integer) As ActionResult
            Dim tasks = From t In db.Tasks Where t.ID = Id
            For Each Match In tasks
                Match.IsCompleted = False
            Next
            db.SubmitChanges()
            Return RedirectToAction("Index")
        End Function
        'del a task
        Function Del(ByVal Id As Integer) As ActionResult
            Dim tasks = From t In db.Tasks Where t.ID = Id
            For Each Match In tasks
                db.Tasks.DeleteOnSubmit(Match)
            Next
            db.SubmitChanges()
            Return RedirectToAction("Index")
        End Function
    End Class
End Namespace